package com.qxzn.app.medicalhealth.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.qxzn.app.api.api.HttpRequest;
import com.qxzn.app.bean.ContactsBean;
import com.qxzn.app.bean.MedicalHealthBean;
import com.qxzn.app.bean.MedicineCaseRecordBean;
import com.qxzn.app.medicalhealth.R;
import com.qxzn.app.medicalhealth.R2;
import com.qxzn.app.medicalhealth.activity.AddMedicalRecordActivity;
import com.qxzn.app.util.DateUtil;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MedicalRecordsAdapter extends RecyclerView.Adapter<MedicalRecordsAdapter.ViewHolder> {

    private Context activity;
    private List<MedicineCaseRecordBean.DataBean> dataList;
    private MedicineCaseRecordBean.DataBean item;

    private View.OnClickListener itemClick = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            MedicineCaseRecordBean.DataBean item = (MedicineCaseRecordBean.DataBean) v.getTag();
            activity.startActivity(new Intent(activity, AddMedicalRecordActivity.class).putExtra("title", AddMedicalRecordActivity.medicalRecordDetails).putExtra("id", item.getId() + ""));
        }
    };

    public MedicalRecordsAdapter(Context activity, List dataList) {
        this.activity = activity;
        this.dataList = dataList;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(activity).inflate(R.layout.item_family_medical_records_content, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        item = dataList.get(position);
        holder.view.setTag(item);
        holder.view.setOnClickListener(itemClick);
        holder.tvDay.setText(DateUtil.formMillisToStr(item.getTime(), "dd"));
        holder.tvMonth.setText(DateUtil.formMillisToStr(item.getTime(), "MM") + "月");
        holder.tvCheckName.setText(item.getCasename());
        List<MedicineCaseRecordBean.DataBean.PicturesBean> picture = item.getPictures();
        if (picture != null && picture.size() > 0) {
            Glide.with(activity).load(HttpRequest.BASE_URL + picture.get(0).getUrl()).apply(new RequestOptions().placeholder(R.mipmap.logo).error(R.mipmap.logo)).into(holder.ivIcon);
        }
    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {

        private View view;
        @BindView(R2.id.iv_icon)
        ImageView ivIcon;
        @BindView(R2.id.tv_day)
        TextView tvDay;
        @BindView(R2.id.tv_month)
        TextView tvMonth;
        @BindView(R2.id.ll_date)
        LinearLayout llDate;
        @BindView(R2.id.tv_check_name)
        TextView tvCheckName;

        ViewHolder(View view) {
            super(view);
            this.view = view;
            ButterKnife.bind(this, view);
        }
    }
}
